package com.txm.websocket;

import common.webSocket.SystemWebSocketHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

/**
 * Created by tangxinmao on 2016/9/12.
 */
@Configuration
@EnableWebSocket
public class WebSocket extends WebMvcConfigurerAdapter implements WebSocketConfigurer {
    @Autowired
    private WebSocketHandshakeInterceptor webSocketHandshakeInterceptor;

    public void registerWebSocketHandlers(WebSocketHandlerRegistry webSocketHandlerRegistry) {
        webSocketHandlerRegistry.addHandler(systemWebSocketHandler(), "/webSocketServer").addInterceptors(webSocketHandshakeInterceptor); //支持websocket 的访问链接
        webSocketHandlerRegistry.addHandler(systemWebSocketHandler(), "/sockjs/webSocketServer").addInterceptors(webSocketHandshakeInterceptor).withSockJS();
    } //不支持websocket的访问链接

    @Bean
    public WebSocketHandler systemWebSocketHandler() {
        return new SystemWebSocketHandler();
    }

}
